﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Framework.Model;
using FantastPlatform.Framework.IModel;
using FantastPlatform.Framework.IService;
using FantastPlatform.Framework.Core;
using FantastPlatform.Framework.Common;

namespace FantastPlatform.Framework.Service
{
    public class PoliceStreetService : DataService<IPoliceStreet, PoliceStreet>, IPoliceStreetService
    {

        #region IPoliceStreetService 成员

        public void AddStreet(int policeId, int streetId)
        {
            IEmployeeService empService = ServiceFactory.CreateService<IEmployeeService>();
            IStreetService streetService = ServiceFactory.CreateService<IStreetService>();
            IEmployee emp = empService.Get(policeId);
            IList<IPoliceStreet> areaStreetList;
            ConditionGroup group = new ConditionGroup();
            group.Conditions.Add(new Condition { Field = "Enable", Type = typeof(bool).FullName, Value1 = true });
            group.Conditions.Add(new Condition { Field = "PoliceMan.Id", Type = typeof(Int32).FullName, Value1 = policeId, Relation = Relations.And });
            if (emp != null)
            {
                IPoliceStreet areaStreet = new PoliceStreet();
                areaStreet.PoliceMan = emp;
                group.Conditions.Add(new Condition { Field = "Street.Id", Type = typeof(Int32).FullName, Value1 = streetId, Relation = Relations.And });
                int totalCount = 0;
                areaStreetList = base.Search(group, null, Int32.MaxValue, 1, out totalCount);
                if (totalCount == 0)
                {
                    areaStreet.Street = streetService.Get(streetId);
                    if (areaStreet.Street != null)
                    {
                        areaStreet.Name = areaStreet.Street.Name;
                        base.Create(areaStreet);
                    }
                }

            }
        }

        public void RemoveStreet(int policeId, List<int> streetIds)
        {
            IList<IPoliceStreet> areaStreetList;
            List<int> areaStreeIds = new List<int>();
            ConditionGroup group = new ConditionGroup();
            group.Conditions.Add(new Condition { Field = "Enable", Type = typeof(bool).FullName, Value1 = true });
            group.Conditions.Add(new Condition { Field = "PoliceMan.Id", Type = typeof(Int32).FullName, Value1 = policeId, Relation = Relations.And });
            foreach (int streetId in streetIds)
            {
                group.Conditions.Add(new Condition { Field = "Street.Id", Type = typeof(Int32).FullName, Value1 = streetId, Relation = Relations.And });
                int totalCount = 0;
                areaStreetList = base.Search(group, null, Int32.MaxValue, 1, out totalCount);
                if (totalCount > 0)
                {
                    foreach (IPoliceStreet item in areaStreetList)
                    {
                        areaStreeIds.Add(item.Id);
                    }

                }
            }
            base.Delete(areaStreeIds);
        }

        #endregion

        #region IPoliceStreetService 成员


        public IList<IStreet> GetStreetByPoliceId(int PoliceId)
        {
            int total = 0;
            IList<IStreet> list = new List<IStreet>();
            ConditionGroup conditionGroup = new ConditionGroup();
            conditionGroup.Conditions.Add(new Condition { Type = typeof(bool).FullName, Field = "Enable", Value1 = true });
            conditionGroup.Conditions.Add(new Condition { Type = typeof(Int32).FullName, Field = "PoliceMan.Id", Value1 = PoliceId, Relation = Relations.And });
            foreach (IPoliceStreet item in base.Search(conditionGroup, null, Int32.MaxValue, 1, out total))
            {
                list.Add(item.Street);
            }
            return list;
        }
        public IList<IStreet> SearchStreet(string code, ConditionGroup group, int pageSize, int pageNum, out int totalRecords)
        {
            Condition condition = new Condition();
            if (group == null)
            {
                group = new ConditionGroup();
                Condition enableCondition = new Condition() { Field = "Enable", Type = typeof(bool).FullName, Value1 = true };
                group.Conditions.Add(enableCondition);

            }
            if (!string.IsNullOrEmpty(code))
            {

                if (Tools.IsChinese(code))
                {
                    condition = new Condition() { Field = "Street.Name", Type = typeof(string).FullName, Value1 = code + "%", Comparison = Comparisons.Like };

                }
                else if (Tools.IsPinyin(code))
                {
                    condition = new Condition() { Field = "Street.Pinyin", Type = typeof(string).FullName, Value1 = code + "%", Comparison = Comparisons.Like };
                }
                else
                    condition = new Condition() { Field = "Street.Code", Type = typeof(string).FullName, Value1 = code + "%", Comparison = Comparisons.Like };
                group.Conditions.Add(condition);
            }
            IList<IPoliceStreet> list = base.Search(group, null, Int32.MaxValue, 1, out totalRecords);
            var s = (from ps in list select ps.Street).Distinct();
            IList<IStreet> slist = s.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList<IStreet>();
            totalRecords = s.Count();
            return slist;

        }


        #endregion
    }
}
